// pages/APPLY/add/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), 
formatTime = require("../../../utils/formatTime.js"),
common_Js = require("../../../component/common.js");
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';

Page({
    /**
     * 页面的初始数据
     */
    data: {
      shopName:'',
      shopSwitch:false,
      searchKey: '',
        apply: {
          reqName:'',
          contactPhone:'',
          applyShopId:0,
          applyShopName:'',
          applyDeviceType:'',
        },
        shopList:[],
        deviceTypeChecked:[],
        user_id:0,
        submitState:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var isLogin = app.getStorageSync("isLogin");
      if (!isLogin) {
        wx.navigateTo({
          url: '../../login/index',
        })
      }else {
        this.data.user_id = app.getStorageSync("user_id");
        this.onHttpInit()
      }
    },

  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    console.log('onHttpInit')
    var that = this;
    fetchApi.httpPost('/shop/shoplist', {
      group_id: 10
    }, function (res) {
      if (res.data.code == 0) {
        that.data.shopList = res.data.data
        that.setData({
          shopList: that.data.shopList
        })
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  onChangeDeviceType(e){
    console.log('onChangeDeviceType',e.detail)
    this.data.deviceTypeChecked = e.detail;
    this.data.apply.applyDeviceType = e.detail.join(',');
    this.setData({
      deviceTypeChecked: this.data.deviceTypeChecked
    })
  },
  
  bindInput(e){
      var key = e.currentTarget.dataset.key;
      if(key == 'req_name'){
        this.data.apply.reqName = e.detail;
      } else if(key == 'contact_phone'){
        this.data.apply.contactPhone = e.detail;
      }
  },
  submitOp(){
    var apply = this.data.apply;
    
    if(apply.regName == ''){
      Toast('请输入姓名/企业名称');return ;
    }
    if(apply.contactPhone == ''){
      Toast('请输联系方式');return ;
    }
    if(apply.applyShopId == ''){
      Toast('请选择申请访问企业');return ;
    }
    if(apply.applyDeviceType == ''){
      Toast('请选择申请访问设备');return ;
    }
    this.onHttpSubmit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpSubmit: function (e) {
    var that = this;
    fetchApi.httpPost('/apply/shop/device/record/add/record', {
      data_json:that.data.apply
    }, function (res) {
      console.log("发起申请响应数据", res)
      if (res.data.code == 0) {
        Toast.success('发起申请成功');
          setTimeout(function(){
            wx.navigateBack({
                delta: -1
            })
        },500)
      }else{
        Toast.fail(res.data.msg);
      }
      // that.setData({
      //    isLoading: false, loading: true, netWork: true,
      // })
    }, function (res) {
        // that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  onSwichClose() {
    this.setData({
      shopSwitch: false
    })
  },
  //点击确定搜索时触发事件
  onSearch() {
    let row = [];
    if (this.data.searchKey.length > 0) {
      this.data.shopList.filter(
        (e) => {
          if (e.shopName.indexOf(this.data.searchKey.toLowerCase()) != -1) {
            row.push(e)
          }
        })
    } else {
      row = this.data.shopList
    }
    this.setData({
      // cognizance: this.data.searchKey,
      shopList: row
    })
  },
  openSwitch(event) {
    this.setData({
      shopSwitch: true,
      searchKey: '',
      shopList: this.data.shopList
    })
  },
  resetColumns() {
    this.setData({
      searchKey: '',
      shopList: this.data.shopList
    })
  },
  shutdown() {
    this.setData({
      shopSwitch: false,
      searchKey: '',
    })
    this.resetColumns()
  },
  identification(event) {
    let that = this
    let shop = event.detail.value
    this.data.apply.applyShopId = shop.id
    this.data.apply.applyShopName = shop.shopName
    this.data.shopName = shop.shopName
    this.setData({
      shopName: this.data.shopName
    })
    this.shutdown()
  },
})